package com.web;

import java.io.*;
import java.net.*;
import java.util.*;

import com.util.ResultData;

public class HttpUtil {
	public static String sendPost(String urlStr,ResultData paramData) throws Exception {
		URL urlObject=new URL(urlStr);
		HttpURLConnection httpUrlConnection=(HttpURLConnection)urlObject.openConnection();
		httpUrlConnection.setRequestMethod("POST");
		httpUrlConnection.setDoOutput(true);

		StringBuffer paramBuffer = new StringBuffer();
		Set<Map.Entry<String, String>> paramSet=paramData.entrySet();
		for (Map.Entry<String, String> param : paramSet) {
			if(paramBuffer.length() > 0) {
				paramBuffer.append("&");
			}
			paramBuffer.append(param.getKey()).append("=").append(param.getValue());
		}
		httpUrlConnection.getOutputStream().write(paramBuffer.toString().getBytes());
		httpUrlConnection.getOutputStream().flush();
		httpUrlConnection.getOutputStream().close();
		//httpUrlConnection.connect();

		InputStream in= httpUrlConnection.getInputStream();
		BufferedReader  rd = new BufferedReader(new InputStreamReader(in));
		StringBuilder response=new StringBuilder();
		char[] buf = new char[1024];
		int read=0;
		while((read=rd.read(buf)) > 0){
			response.append(buf,0,read);
		}
		return response.toString();
	}
}
